import formJs from "./regexDetail.js"; //新增弹框配置项
// import publicForm from "@/components/public/up-form.vue"; //弹框form
import publicForm from "@/components/public/el-form.vue"; //弹框form

import {
    saveBeamInfo, //保存
    getBeamInfo, //详情
} from "@/api/BackStage/stationProductionPage/request.js";

export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
    ],
    data() {
        return {
            activeNames: ["0", "1", "2", "3", "4", "5"],
            visible: "",
            title: null,
            isLock: false,

            dialogHeight: null,
            activeIndex: "1",

            formData: {
                fId: "", //传fid表示修改 不传表示新增

                fPouringdates: "",// 浇筑日期
                fPouringcontent: "",// 浇筑填报说明
                fPouringuserid: "",// 浇筑填报人id
                fPouringusername: "",// 浇筑填报人名称
                fPouringfile: "",//浇筑文件id
                pouringFile: "",//浇筑文件

                fRebounddates: "",// 回弹检测日期
                fReboundcontent: "",// 回弹检测填报说明
                fRebounduserid: "",// 回弹检测填报人id
                fReboundusername: "",// 回弹检测填报人名称
                fReboundfile: "",//回弹文件id
                reboundFile: "",//回弹文件

                fProtectdates: "",// 钢保检测日期
                fProtectcontent: "",// 钢保检测填报说明
                fProtectuserid: "",// 钢保检测填报人id
                fProtectusername: "",// 钢保检测填报人名称
                fProtectfile: "",//钢保文件id
                protectFile: "",//钢保文件

                fTensioningdates: "",// 张拉日期
                fTensioningcontent: "",// 张拉填报说明
                fTensioninguserid: "",// 张拉填报人id
                fTensioningusername: "",// 张拉填报人名称
                fTensioningfile: "",//张拉文件id
                tensioningFile: "",//张拉文件

                fGroutingdates: "",// 压浆日期
                fGroutingcontent: "",// 压浆填报说明
                fGroutinguserid: "",// 压浆填报人id
                fGroutingusername: "",// 压浆填报人名称
                fGroutingfile: "",//压浆文件id
                groutingFile: "",//压浆文件


                fContent: "",// 说明

                userId: "",// 当前用户id
                userName: "",// 当前用户名称

                recordfiles: []// 删除文件集合
            },
            formObj: formJs.formObj, //浇筑
            reboundObj: formJs.reboundObj, //回弹
            rebarObj: formJs.rebarObj, //钢保
            tensionObj: formJs.tensionObj, //张拉
            groutObj: formJs.groutObj, //压浆
            contentObj: formJs.contentObj, //说明


            fileList: [],
            fileList1: [],
            fileList2: [],
            fileList3: [],
            fileList4: [],
            btnShow: true,
            currentDetail: null,
            timeYMD: "",
            timeYmdHms: "",
        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },

        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }

            },
            immediate: true,
        },

        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                }
            },
            immediate: true,
        },


    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
        this.getData();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },

    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        this.btnShow = true;

        this.formObj[2].uploadSelect = true;
        this.reboundObj[2].uploadSelect = true;
        this.rebarObj[2].uploadSelect = true;
        this.tensionObj[2].uploadSelect = true;
        this.groutObj[2].uploadSelect = true;

        if (this.statusNum == 1) { //编辑
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.reboundObj.map((item) => {
                item.disabled = false;
            });
            this.rebarObj.map((item) => {
                item.disabled = false;
            });
            this.tensionObj.map((item) => {
                item.disabled = false;
            });
            this.groutObj.map((item) => {
                item.disabled = false;
            });
            this.contentObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[0].disabled = true;
            this.reboundObj[0].disabled = true;
            this.rebarObj[0].disabled = true;
            this.tensionObj[0].disabled = true;
            this.groutObj[0].disabled = true;
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false;
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.reboundObj.map((item) => {
                item.disabled = true;
            });
            this.rebarObj.map((item) => {
                item.disabled = true;
            });
            this.tensionObj.map((item) => {
                item.disabled = true;
            });
            this.groutObj.map((item) => {
                item.disabled = true;
            });
            this.contentObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[2].uploadSelect = false;
            this.reboundObj[2].uploadSelect = false;
            this.rebarObj[2].uploadSelect = false;
            this.tensionObj[2].uploadSelect = false;
            this.groutObj[2].uploadSelect = false;
        }
        this.getDetail({
            fId: this.currentDetail.fId
        })
        this.getHeight();
    },
    methods: {
        // 折叠面板
        changeCollapse(e) {
            console.log(this.activeNames, e);
        },
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            console.log("selectChange", data, a, b);
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        // 详情
        getDetail(obj) {
            var that = this
            getBeamInfo(obj).then((res) => {
                if (res.errcode == 0) {
                    that.formData = res.data.precastBeamInfo;
                    if (that.formData.fPouringusername == "" || that.formData.fPouringusername == null) {
                        that.formData.fPouringusername = that.userInfo.realName;
                        that.formData.fPouringuserid = that.userInfo.userId;
                    }
                  
                    if (that.formData.fReboundusername == "" || that.formData.fReboundusername == null) {
                        that.formData.fReboundusername = that.userInfo.realName;
                        that.formData.fRebounduserid = that.userInfo.userId;
                    }

                    if (that.formData.fProtectusername == "" || that.formData.fProtectusername == null) {
                        that.formData.fProtectusername = that.userInfo.realName;
                        that.formData.fProtectuserid = that.userInfo.userId;
                    }

                    if (that.formData.fTensioningusername == "" || that.formData.fTensioningusername == null) {
                        that.formData.fTensioningusername = that.userInfo.realName;
                        that.formData.fTensioninguserid = that.userInfo.userId;
                    }

                    if (that.formData.fGroutingusername == "" || that.formData.fGroutingusername == null) {
                        that.formData.fGroutingusername = that.userInfo.realName;
                        that.formData.fGroutinguserid = that.userInfo.userId;
                    }
                    that.formData.recordfiles=[];
                    // 文件
                    that.fileList = res.data.pouringFile;
                    that.fileList1 = res.data.reboundFile;
                    that.fileList2 = res.data.protectFile;
                    that.fileList3 = res.data.tensioningFile;
                    that.fileList4 = res.data.groutingFile;
                    // 文件

                }
            });
        },
        // 刪除文件
        delteFile(file, num) {
            console.log("delteFile",file, num);
            var that=this;
            if (num == 0) {
                for (let index = 0; index < that.fileList.length; index++) {
                    if (file.uid == that.fileList[index].uid) {
                        if (that.fileList[index].atta == undefined) {
                            that.formData.recordfiles.push({
                                fId: that.fileList[index].fId,
                            });
                        } else {
                            that.formData.recordfiles.push({
                                fId: that.fileList[index].atta.fId,
                            });
                        }
                        that.fileList.splice(index, 1);
                    }
                }
            } else if (num == 1) {
                for (let index = 0; index < that.fileList1.length; index++) {
                    if (file.uid == that.fileList1[index].uid) {
                        if (that.fileList1[index].atta == undefined) {
                            that.formData.recordfiles.push({
                                fId: that.fileList1[index].fId,
                            });
                        } else {
                            that.formData.recordfiles.push({
                                fId: that.fileList1[index].atta.fId,
                            });
                        }
                        that.fileList1.splice(index, 1);
                    }
                }
            } else if (num == 2) {
                for (let index = 0; index < that.fileList2.length; index++) {
                    if (file.uid == that.fileList2[index].uid) {
                        if (that.fileList2[index].atta == undefined) {
                            that.formData.recordfiles.push({
                                fId: that.fileList2[index].fId,
                            });
                        } else {
                            that.formData.recordfiles.push({
                                fId: that.fileList2[index].atta.fId,
                            });
                        }
                        that.fileList2.splice(index, 1);
                    }
                }
            } else if (num == 3) {
                for (let index = 0; index < that.fileList3.length; index++) {
                    if (file.uid == that.fileList3[index].uid) {
                        if (that.fileList3[index].atta == undefined) {
                            that.formData.recordfiles.push({
                                fId: that.fileList3[index].fId,
                            });
                        } else {
                            that.formData.recordfiles.push({
                                fId: that.fileList3[index].atta.fId,
                            });
                        }
                        that.fileList3.splice(index, 1);
                    }
                }
            } else if (num == 4) {
                for (let index = 0; index < that.fileList4.length; index++) {
                    if (file.uid == that.fileList4[index].uid) {
                        if (that.fileList4[index].atta == undefined) {
                            that.formData.recordfiles.push({
                                fId: that.fileList4[index].fId,
                            });
                        } else {
                            that.formData.recordfiles.push({
                                fId: that.fileList4[index].atta.fId,
                            });
                        }
                        that.fileList4.splice(index, 1);
                    }
                }
            }

        },
        // 保存
        yj_saveForm() {
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }

        },
        saveForm(obj) {
            console.log(3);
            this.isLock = true
            saveBeamInfo(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log("拌合站设置-新增）", error);
                });
        },
        // 保存
        // 时间
        getData() {
            var time = new Date();
            const y = time.getFullYear();
            const m = (time.getMonth() + 1 + "").padStart(2, "0");
            const d = (time.getDate() + "").padStart(2, "0");
            const hh = (time.getHours() + "").padStart(2, "0");
            const mm = (time.getMinutes() + "").padStart(2, "0");
            const ss = (time.getSeconds() + "").padStart(2, "0");
            this.timeYMD = `${y}-${m}-${d}`;
            this.timeYmdHms = `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
        },

    },
};